package edu.pitt.dbmi.odie.gapp.gwt.server.document;

import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.ExtensionProfile;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.gdata.util.common.xml.XmlWriter;

import edu.pitt.dbmi.odie.gapp.gwt.server.util.gdata.ODIE_DocsServiceFetcher;
import edu.pitt.dbmi.odie.gapp.gwt.server.util.gdata.ODIE_GDataDocsService;
import edu.pitt.dbmi.odie.gapp.gwt.server.util.rest.ODIE_NcboRestUtils;

// edu.pitt.dbmi.odie.gapp.gwt.server.foundry.ODIE_FoundryServlet

@SuppressWarnings("serial")
public class ODIE_DocumentServlet extends HttpServlet {

	private static final String GDOCS_DEF_FEED_HOST = "docs.google.com";
	private static final String GDOCS_FOLDERS_LIST = "/feeds/documents/private/full/-/folder";
//	private static final String GDOCS_FOLDERS_LIST = "/feeds/documents/private/full/-/folder?prettyprint=true";
	public static final String GDOCS_FEED_URL_PATH = "/feeds/documents/private/full";

	public static final int DEFAULT_BUFFER_SIZE = 1024;

	private ServletConfig config = null;

	public void init(ServletConfig config) {
		this.config = config;
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String payLoadXml = ODIE_NcboRestUtils.getXmlHeader() + "<atom:payLoad xmlns:atom='http://www.w3.org/2005/Atom'/>" ;
		try {
			ODIE_GDataDocsService gDataService = ODIE_DocsServiceFetcher
					.fetchDocsService(this.config);
			if (gDataService != null && gDataService.getDocsService() != null) {
				DocsService docsService = gDataService.getDocsService();
				DocumentListFeed feed;
				URL documentListFeedUrl = new URL("http://"
						+ GDOCS_DEF_FEED_HOST + GDOCS_FOLDERS_LIST);
				documentListFeedUrl = new URL("http://docs.google.com/feeds/default/private/full/-/folder") ;
				// These System Properties turn off a warning message from within the GData documents service
				System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
				System.setProperty ("org.apache.commons.logging.simplelog.showdatetime", "true");
				System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "error");
				feed = docsService.getFeed(documentListFeedUrl,
						DocumentListFeed.class);
				String feedXml = convertToXml(feed);
				payLoadXml = ODIE_NcboRestUtils.getXmlHeader() + feedXml;
			}
		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			resp.getWriter().write(payLoadXml);
			resp.getWriter().close();
		}
	}

	private String convertToXml(DocumentListFeed feed) throws IOException {
		StringWriter sw = new StringWriter();
		XmlWriter diagnosticXmlWriter = new XmlWriter(sw);
		ExtensionProfile arbitraryProfile = new ExtensionProfile();
		arbitraryProfile.setArbitraryXml(true);
		feed.generate(diagnosticXmlWriter, arbitraryProfile);
		return sw.toString();
	}

}
